package com.zvz.server.ws.impl;

import com.zvz.entity.User;
import com.zvz.server.ws.HelloWService;

import java.util.Date;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;


@WebService(
        endpointInterface = "com.zvz.server.ws.HelloWService",
        portName = "HelloWSPort",
        serviceName = "HelloWSService",
        targetNamespace = "http://www.tmp.com/ws/hello")
public class HelloWServiceImpl implements HelloWService {

    @Resource
    private WebServiceContext wsContext;

    public String index() {
        return "hello";
    }

    public Integer add(Integer x, Integer y) {
        return x + y;
    }

    public Date now() {
        return new Date();
    }

    public User getPerson(String username, String password) {

        System.out.println(getClientInfo());
        User user = new User();
        user.setUsername("名字");
        user.setPassword("密码");
        return user;
    }

    private String getClientInfo() {
        String clientIP = null;
        try {
            MessageContext mc = wsContext.getMessageContext();
            HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));
            clientIP = request.getRemoteAddr();
            System.out.println("client IP : " + clientIP);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return clientIP;

    }

  /*  webservice 客户端生成：
    格式：wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”
*/
}
